const cheerio = require("cheerio");
const fs = require("fs");
const axios = require("axios").default;
// const $ = cheerio.load();     
// console.log($('h1').text());

// axios.get("http://www.baidu.com").then((res) =>{
//     const $ = cheerio.load(res.data);
//     console.log($("title").text());
// });
const books = [];
axios.get("https://www.17k.com/top/refactor/top100/01_subscribe/01_subscribe_newBook_top_100_pc.html").then((res) => {
    const $ = cheerio.load(res.data);
    $(".BOX").eq(0).find("table tr").each(function (index) {
        if (index > 0) { 
            // console.log($(this).find("td").eq(2).find("a").text());
            let book ={}
            book.id = index;
            book.title = $(this).find("td").eq(2).find("a").text();
            book.link = "https:" + $(this).find("td").eq(2).find("a").attr("href");
            fs.mkdirSync("./books/" + book.title,{
                recursive:true
            })
            books.push(book)
        }
    })
    fs.writeFileSync("./books.json",JSON.stringify(books));
    
})
